﻿using Duende.IdentityServer.Validation;
using Simple.Tools;
using SimpleSaas.Entity.Dto;
using SimpleSaasAuth.Services;
using System.DirectoryServices;

namespace SimpleSaasAuth.Configuration.Validator
{
	public class CustomResourceOwnerPasswordValidator : IResourceOwnerPasswordValidator
	{
		private readonly IUserVerifyService userVerifyService;
		public CustomResourceOwnerPasswordValidator(IUserVerifyService userVerifyService)
		{
			this.userVerifyService = userVerifyService;
		}

		/// <summary>
		/// 密码登录验证
		/// </summary>
		/// <param name="context"></param>
		/// <returns></returns>
		public async Task ValidateAsync(ResourceOwnerPasswordValidationContext context)
		{
			context.Result = await this.userVerifyService.LoginAsync(
				new LoginUserDto()
				{
					Account = context.UserName,
					Password = context.Password,
					AuthenticateType = 1
				},
			  "用户名或者密码错误!");
		}
	}
}